import random

setGlobalDuration(-1)

env2 = genAdsr(release=0.8)

line = genLineseg([0, 150, 0, 1000, 1, 0, 0, 150, 0, 1000, 1])
readTable(bus='ind', table=line, duration=30)

tt1 = genDataTable([1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0])
tt2 = genDataTable([0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0])
tt3 = genDataTable([1,0,1,0,1,1,1,0,1,0,0,0,1,0,1,1])
tt4 = genDataTable([1,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0])
tt5 = genDataTable([1,0,1,0,1,0,1,0,1,1,0,0,1,0,0,0])
tt6 = genDataTable([1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0])
tapTable = tableMorphing(tablelist=[tt1, tt2, tt3], indexVar='ind', length=16)

pi1 = genDataTable([1,1,1,1,.5,1,1,1,.25,1,1,1,.125,1,1,1])
pi2 = genDataTable([.5,1,.5,1,.5,1,.5,1,.25,1,.5,1,.25,1,.5,1])
pi3 = genDataTable([.5,.5,.25,.5,.5,.5,.25,.5,.25,.5,.5,.5,.25,.5,.5,.25])
pitTable = tableMorphing(tablelist=[pi1, pi2, pi3], indexVar='ind', length=16)

randomi(bus=[1,2,3,4,5,6], mini=.99, maxi=1.01, rate=[.5,1,2,3,5,8])

oscReceive(bus='key', address='/key', port=8000)

metro(bus='metro', tempo=132)

beginSequencer(input='metro', active=0, trigbus='key', trigval=49, table=tt1)
notes = 10
pitchs = [random.randint(40,72)*0.25 for x in range(notes)]
durs = [random.randint(1,10)*.2 for x in range(notes)]
pluckedString(pitch=pitchs, amplitude=.04, starttime=0, duration=durs, pan=[0,.25,.5,.75,1], pitchVar=[1,2,3,4,5,6])
endSequencer()

beginSequencer(input='metro', active=0, trigbus='key', trigval=50, table=tt2)
notes = 10
pitchs = [random.randint(180,184)*1 for x in range(notes)]
durs = [random.randint(1,10)*.2 for x in range(notes)]
pluckedString(pitch=pitchs, amplitude=.05, starttime=0, duration=durs, pan=[0,.25,.5,.75,1])
endSequencer()

beginSequencer(input='metro', active=0, trigbus='key', trigval=51, table=tt3)
notes = 20
pitchs = [random.randint(480,8850) for x in range(notes)]
durs = [random.randint(1,10)*.1 for x in range(notes)]
sine(pitch=pitchs, amplitude=.02, starttime=0, duration=durs, envelope=env2, pan=[0,.25,.5,.75,1], pitchVar=[1,2,3,4,5,6])
endSequencer()

beginSequencer(input='metro', active=0, trigbus='key', trigval=52, table=tt4)
sequencerPitchTable(pitTable)
notes = 10
pitchs = [random.randint(720,760) for x in range(notes)]
durs = [random.randint(1,10)*.1 for x in range(notes)]
square(pitch=pitchs, amplitude=.05, starttime=0, duration=durs, envelope=env2, pan=[0,.25,.5,.75,1], pitchVar=[1,2,3,4,5,6])
endSequencer()

beginSequencer(input='metro', active=0, trigbus='key', trigval=53, table=tt1)
notes = 10
pitchs = [random.randint(180,190)*0.125 for x in range(notes)]
durs = [random.randint(1,10)*.1 for x in range(notes)]
readTable(bus='index', table=env2, duration=durs[0])
freqMod(pitch=pitchs, amplitude=.07, starttime=0, duration=durs, envelope=env2, pan=[0,.25,.5,.75,1], index=30, indexVar='index')
endSequencer()

beginSequencer(input='metro', active=0, trigbus='key', trigval=54, table=tapTable)
sequencerPitchTable(pitTable)
notes = 10
pitchs = [random.randint(180,190)*2 for x in range(notes)]
durs = [random.randint(1,10)*.05 for x in range(notes)]
readTable(bus='index2', table=env2, duration=durs[0])
freqMod(pitch=pitchs, amplitude=.07, starttime=0, duration=durs, envelope=env2, pan=[0,.25,.5,.75,1], index=10, indexVar='index2')
endSequencer()

startCsound()


def seq1(val):
    sendOscTrigger(49, address='/key')

def seq2(val):
    sendOscTrigger(50, address='/key')

def seq3(val):
    sendOscTrigger(51, address='/key')

def seq4(val):
    sendOscTrigger(52, address='/key')

def seq5(val):
    sendOscTrigger(53, address='/key')

def seq6(val):
    sendOscTrigger(54, address='/key')

frame = beginGUI('Sequencer', (520,150))
makeToggle(frame, 'Seq 1', (20,50), seq1)
makeToggle(frame, 'Seq 2', (100,50), seq2)
makeToggle(frame, 'Seq 3', (180,50), seq3)
makeToggle(frame, 'Seq 4', (260,50), seq4)
makeToggle(frame, 'Seq 5', (340,50), seq5)
makeToggle(frame, 'Seq 6', (420,50), seq6)
endGUI(frame)